﻿using System;
using System.Collections.Generic;
using System.Text;
using MirrorDrive.Operations;

namespace MirrorDrive.Server.Authorization
{
    public class BasicAuth : AuthorizationBase
    {
        private SortedList<string, UserInfo> _dataBase = new SortedList<string, UserInfo>();

        public override AuthorizationResponse Authorize(string username, string password)
        {
            lock (_dataBase)
            {
                if (!_dataBase.ContainsKey(username))
                    return base.MakeResponse(false, DriveAccess.None, "");

                if (_dataBase[username].Password != password)
                    return base.MakeResponse(false, DriveAccess.None, "");

                return base.MakeResponse(true, _dataBase[username].Access, _dataBase[username].Path);
            }
        }

        public void AddUser(string username, string password, string path, DriveAccess access)
        {
            lock (_dataBase)
            {
                UserInfo info = new UserInfo();
                info.Access  = access;
                info.Password = password;
                info.Path = path;
                info.Username = username;

                _dataBase.Add(username, info);
            }
        }

        private class UserInfo
        {
            public string Username { get; set; }
            public string Password { get; set; }
            public DriveAccess Access { get; set; }
            public string Path { get; set; }
        }
    }

}
